package com.oa.common.aspect;

import com.oa.modules.approvalsec1.entity.BiddProjectEntity;
import com.oa.modules.approvalsec1.service.BiddProjectService;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: Hankaicheng
 * @Date: 2022/5/17 11:30
 */
@Aspect
@Component
@Log4j2
public class CheckProjectStatusAspect {

    @Autowired
    private BiddProjectService biddProjectService;

    @Pointcut("@annotation(com.oa.common.annotation.CheckProjectStatus)")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void around() {
        List<BiddProjectEntity> list = biddProjectService.list();
        ArrayList<BiddProjectEntity> waiUpdate = new ArrayList<>();
        list.forEach(e->{
            if(e.getStartTime()!=null&&new Date().getTime()> e.getStartTime().getTime()&&e.getStatus()==1){
                e.setStatus(2);
                waiUpdate.add(e);
            }
        });
        if(waiUpdate!=null&&waiUpdate.size()>0){
            biddProjectService.updateBatchById(waiUpdate);
        }
        log.error("run this mehtod");
    }

}
